package dbTools.factory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import dbTools.tableInfo.field.Field;
import dbTools.tableInfo.field.Config.IMEType;
import dbTools.tableInfo.field.fieldValidator.FieldValidator;
import dbTools.tableInfo.field.fieldValidator.validators.*;
import dbTools.tableInfo.field.fields.StringField;

public class SimpleFactory {

	public final static LinkedHashMap<String,IMEType> imeTypes = new LinkedHashMap<String,IMEType> (); 
	public final static LinkedHashMap<String,FieldValidator> validators = new LinkedHashMap<String,FieldValidator> ();
	public final static LinkedHashMap<IMEType,List<String>> imeTypes_validators = new LinkedHashMap<IMEType,List<String>> (); 
    static{  
    	//imeTypes
    	imeTypes.put("【文】半角", IMEType.var_half);
    	imeTypes.put("【文】全角半角", IMEType.var_fullHalf);
    	imeTypes.put("【文】全角のみ", IMEType.var_full);
    	imeTypes.put("【文】半角カナ", IMEType.var_halfKana);
    	imeTypes.put("【文】全角カナ", IMEType.var_fullKana);
    	imeTypes.put("【文】Unicode", IMEType.var_unicode);
    	imeTypes.put("【数】bool", IMEType.num_bool);
    	imeTypes.put("【文】bool", IMEType.var_bool);
    	imeTypes.put("【数】年月", IMEType.num_yearMonth);
    	imeTypes.put("【数】日付", IMEType.num_date);
    	imeTypes.put("【数】時刻", IMEType.num_time);
    	imeTypes.put("【数】日時", IMEType.num_dateTime);
    	imeTypes.put("【数】数値", IMEType.num_number);
    	imeTypes.put("【数】選択", IMEType.num_choose);
    	imeTypes.put("【時】年月", IMEType.date_yearMonth);
    	imeTypes.put("【時】日付", IMEType.date_date);
    	imeTypes.put("【時】時刻", IMEType.date_time);
    	imeTypes.put("【時】日時", IMEType.date_dateTime);
    	
    	//validators
    	validators.put(FieldValidator.DATA_FIELD, new DateFieldValidator());
    	validators.put(FieldValidator.DATA_RANGE_SN, new DateRangeValidator_SN());
    	validators.put(FieldValidator.DATA_RANGE, new DateRangeValidator());
    	validators.put(FieldValidator.EMAIL, new EmailValidator());
    	validators.put(FieldValidator.FAX, new FaxFieldValidator());
    	validators.put(FieldValidator.HALF_NUMERIC, new HalfNumericValidator());
    	validators.put(FieldValidator.HALF_STRING, new HalfStringValidator());
    	validators.put(FieldValidator.MOBILE, new MobileValidator());
    	validators.put(FieldValidator.NOT_HALF_HIRAKANA, new NotHalfHirakanaValidator());
    	validators.put(FieldValidator.NOT_HALF_KATAKANA, new NotHalfKatakanaValidator());
    	validators.put(FieldValidator.NOT_HALF_STRING, new NotHalfStringValidator());
    	validators.put(FieldValidator.REQUIRED_DEPEND, new RequiredDependValidator());
    	validators.put(FieldValidator.REQUIRED_STRING, new RequiredStringValidator());
    	validators.put(FieldValidator.REQUIRED, new RequiredValidator());
    	validators.put(FieldValidator.SHORT_REQUIRED, new ShortCircuitRequiredValidator());
    	validators.put(FieldValidator.SORT_MSG, new SortMsgValidator());
    	validators.put(FieldValidator.STRING_BYTE, new StringByteValidator());
    	validators.put(FieldValidator.TELEPHONE, new TelephoneValidator());
    	validators.put(FieldValidator.URL, new URLValidator());
    	
    	//impType get validator
    	imeTypes_validators.put(IMEType.var_half, new ArrayList<String>(){{
    											add(FieldValidator.HALF_STRING);}});
    	imeTypes_validators.put(IMEType.var_full, new ArrayList<String>(){{
												add(FieldValidator.NOT_HALF_STRING);}});
    	imeTypes_validators.put(IMEType.var_halfKana, new ArrayList<String>(){{}});
    	imeTypes_validators.put(IMEType.var_fullKana, new ArrayList<String>(){{
    											add(FieldValidator.NOT_HALF_HIRAKANA);}});
    }  
    
    
	
	public static <T> T getReflectObj(Class<T> entityClass,Object... param){
    	Object bean=null;
		try {
			Class classType = Class.forName(entityClass.getName().toString());		
			if(param.length>0)
			{
				Class[] paraTypes = new Class[param.length];
				for(int i=0;i<paraTypes.length;i++){
					paraTypes[i]=String.class;
				}
				Constructor constructor = classType.getDeclaredConstructor(paraTypes); //找到指定的构造方法
	            constructor.setAccessible(true);//设置安全检查，访问私有构造函数必须
	            
	            try{
	            	bean =(T) constructor.newInstance(param);
	            }catch(Exception e){
	            	
	            	e.printStackTrace();
	            }
	            
	            return (T)bean;
			}else{
				return (T) classType.newInstance();
			}
			

            
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			
			e.printStackTrace();
		}
    	return (T)bean;
    }
	
	public static <T> T getReflectObj(Class<T> entityClass){
    	Object bean=null;
		try {
			Class classType = Class.forName(entityClass.getName().toString());		

			return (T) classType.newInstance();

		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			
			e.printStackTrace();
		}
    	return (T)bean;
    }
	
	public static IMEType getIMEType(String name)
	{
		if(imeTypes.containsKey(name))
		{
			return imeTypes.get(name);
		}
		
		return IMEType.var_half;
	}
	
	public static FieldValidator getFieldValidator(String key)
	{
		if(validators.containsKey(key))
		{
			return validators.get(key);
		}
		return null;
	}
	
	public static List<FieldValidator> getFieldValidators(Field field)
	{
		List<FieldValidator> validators=new ArrayList<FieldValidator>();
		
		//requried
		if(field.isNotNull())
		{
			//if(field.getDataTypeString()!=null && field.getDataTypeString().equals(Field.TYPE_STRING))
			if(field instanceof StringField)
			{
				validators.add(getFieldValidator(FieldValidator.REQUIRED_STRING));
			}
			else
			{
				validators.add(getFieldValidator(FieldValidator.REQUIRED));
			}
		}
		
		//By imeTypes
		List <String> validatorsKey=imeTypes_validators.get(field.getImeType());
		if(validatorsKey!=null)
		{
			for(String key : validatorsKey)
			{
				validators.add(getFieldValidator(key));
			}
		}
		
		//length
		
		// by fieldInfo
		
		return validators;
	}
	
}
